package com.beiding.controller;

import com.beiding.pojo.Evaluation;
import com.beiding.pojo.Room;
import com.beiding.service.EvaluationService;
import com.beiding.service.RoomStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;
import java.util.List;


@RestController
public class StorageController {

    private RoomStorageService roomStorageService;

    private EvaluationService evaluationService;

    @Autowired
    public void setEvaluationService(EvaluationService evaluationService) {
        this.evaluationService = evaluationService;
    }

    @Autowired
    public void setRoomStorageService(RoomStorageService roomStorageService) {
        this.roomStorageService = roomStorageService;
    }

    //发布房源
    @PostMapping("/publish")
    @Secured("ROLE_LANDLORD")
    boolean publish(Principal principal, String roomId, Boolean publishState) {
        //如果方法能够正常执行,结果将会令房源的实际状态与期望的状态一致
        roomStorageService.publish(principal, roomId, publishState);
        return publishState;
    }


    @GetMapping("/favoriteRooms")
    @Secured("ROLE_TENANT")
    List<Room> favoriteRooms(Principal principal) {
        return roomStorageService.getFavoriteRooms(principal.getName());
    }

    //收藏房源
    @PostMapping("/favorite")
    @Secured("ROLE_TENANT")
    Boolean favorite(Principal principal, String id, Boolean state) {
        roomStorageService.favorite(principal.getName(), id, state);
        return state;
    }

    //是否收藏了房源
    @PostMapping("/isFavorite")
    @Secured("ROLE_TENANT")
    Boolean isFavorite(Principal principal, String id) {
        return roomStorageService.isFavorite(principal.getName(), id);
    }

    /**
     * 获取一个简要信息的房源
     */

    @GetMapping("/get/brief")
    @Secured("ROLE_TENANT")
    Room roomBrief(@RequestParam String id) {
        return roomStorageService.getRoom(id);
    }


    /**
     * 获取关于某个房源的评论,默认每页加载10个
     */
    @GetMapping("/evaluations")
    @Secured("ROLE_TENANT")
    List<Evaluation> evaluations(String id, @PageableDefault(size = 10) Pageable pageable) {
        return evaluationService.get(id, pageable);
    }


    /**
     * 获取附近房源
     * <p>
     * 该方法用于在近似方形的范围内获取附近的房源.至于对房源的排序等工作由前端进行处理
     * 每次最大获取房源数量不能多于9个,为了精确获取有效房源,前端控制查找范围
     */
    @GetMapping("/nearby")
    @Secured("ROLE_TENANT")
    List<Room> nearby(double elng, double wlng, double nlat, double slat, @PageableDefault(size = 9) Pageable pageable) {
        return roomStorageService.nearby(elng, wlng, nlat, slat, pageable);
    }


    /*

    房间的状态有三种:

        1.已发布(published)
        2.未发布(unpublished)
        3.使用中(using)


     */

    //房间的状态
    @GetMapping("/state")
    String state(String roomId) {

        //获取当前房间的状态.使用缓存
        return roomStorageService.getState(roomId);
    }


}
